package com.digiwin.escloud.test.core.common;

import com.digiwin.escloud.test.core.constant.CommonConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author yanggld
 * @Date 2020/5/13-10:44
 */
@Slf4j
public class CommonExpressionParser implements IExpressionParser {

	@Override
	public String parseExpression(String source) {
		source = parseData(source);
		source = parseMethod(source);
		return source;
	}

	/**
	 * 获取需要在数据表达式中替换的 共享数据
	 *
	 * @return
	 */
	@Override
	public ITestSharedData getITestSharedData() {
		return new CommonData();
	}

	static String dataReg = "(\\$\\{(.*?)\\})";
	static Pattern dataPattern = Pattern.compile(dataReg);

	/**
	 * 数据的的匹配规则,表达式为 ${}
	 * 从ITestSharedData中的Map<String,Object>匹配数据.
	 * 原始:String value = "{\"annId\":\"${annId}\"}";
	 * ITestSharedData中包含 {"annId":"YOUr_DATA"}
	 * 结果:String value = "{\"annId\":\"YOUr_DATA\"}";
	 */
	@Override
	public String parseData(String source) {
		if (!StringUtils.contains(source, CommonConstants.DATA_EXPRESS_PREFIX)) {
			return source;
		}
		Matcher matcher = dataPattern.matcher(source);
		while (matcher.find()) {
			String expression = matcher.group();
			if (StringUtils.contains(source, expression)) {
				String expressionKey = StringUtils.substringBetween(expression, CommonConstants.DATA_EXPRESS_PREFIX, CommonConstants.DATA_EXPRESS_SUFFIX);
				Object expressionValue = getITestSharedData().getShareData().get(expressionKey);
				if (Objects.isNull(expressionValue)) {
					log.warn("表达式:{},未匹配到对应的数据获取逻辑.",expression);
				} else {
					source = source.replace(expression,expressionValue.toString());
				}
			}
		}
		return source;
	}

	static String methodReg = "(#\\{(.*?)})";
	static Pattern methodPattern = Pattern.compile(methodReg);

	/**
	 * 方法的匹配规则,表达式为 #{}
	 * 原始:String value = "{\"annId\":\"#{random(8)}\"}";
	 * 通过methodInvoke方法进行匹配,翻译成对应的数据
	 * {@link #methodInvoke(String)}
	 * 结果:String value = "{\"annId\":\"sCeaFhgT\"}";
	 */

	@Override
	public String parseMethod(String source) {
		Matcher matcher = methodPattern.matcher(source);
		while (matcher.find()) {
			String expression = matcher.group();
			String expressionKey = StringUtils.substringBetween(expression, CommonConstants.METHOD_EXPRESS_PREFIX, CommonConstants.METHOD_EXPRESS_SUFFIX);
			String expressValue = methodInvoke(expressionKey);
			if (StringUtils.equalsIgnoreCase(expressionKey,expressValue)) {
				log.warn("表达式:{},未匹配到对应的方法处理逻辑.",expression);
			} else {
				source = source.replace(expression, Objects.isNull(expressValue) ? "" : expressValue);
			}
		}
		return source;
	}

	static String methodImplReg = "(random\\((\\d+)\\))";
	static Pattern methodImplPattern = Pattern.compile(methodImplReg);

	/**
	 * 解析方法表达式并调用自定义的逻辑 实现了:
	 * random(num): num为数字,生成num个数的包含字母和数据随机字符串
	 *
	 * @param source
	 * @return
	 */
	@Override
	public String methodInvoke(String source) {
		Matcher matcher = methodImplPattern.matcher(source);
		if (matcher.find()) {
			String expression = matcher.group();
			String randomNum = StringUtils.substringBetween(expression, "random(", ")");
			String randomStr = RandomStringUtils.randomAlphanumeric(Integer.parseInt(randomNum));
			source = source.replace(expression, Objects.isNull(randomStr) ? "" : randomStr);
		}
		return source;
	}
}
